import re
import requests
from parsel import Selector
import time


def parse_number(div_html, response_str):
    div_list = div_html.xpath("./div")
    # print(div_list)
    if len(div_list) < 3:
        div = div_list[-1]
        class_name = div.xpath("./@class").extract_first()
        # print(class_name)
        pattern = '.%s:before { content:"(.*?)" }' % class_name
        # print(re.search(pattern,response_str,re.S).group())
        num = re.findall(pattern, response_str, re.S)[0]
        # print(num)
        if not num:
            num = parse_offset(div_list=div_list, response_str=response_str)
        return int(num)

    elif len(div_list) == 3 or len(div_list) == 4:
        num = parse_offset(div_list=div_list, response_str=response_str)
        return num

    else:
        print('Error...')


def parse_offset(div_list, response_str):

    if len(div_list) == 4:
        real_div = div_list[1:]
        num = ['0','0','0']

    else:
        real_div = div_list
        if len(real_div) == 3:
            num = ['0','0','0']
        elif len(real_div) == 2:
            num = ['0','0']
        elif len(real_div) == 1:
            num = ['0']
        else:
            num = []

    pattern_opacity = r'.%s { opacity:0 }'
    pattern_left = r'.%s { left:(.*?)em }'

    for index,div in enumerate(real_div):
        class_name = div.xpath("./@class").extract_first()
        content = div.xpath("./text()").extract_first()
        is_opacity = re.search(pattern_opacity % class_name,response_str,re.S)
        is_left = re.findall(pattern_left % class_name,response_str,re.S)  # 偏移情况
        # print(is_opacity)
        # print(is_left)

        if is_opacity:
            num[index] = '-'
            continue

        if not is_left:
            num[index] = content
            continue

        else:
            left_num = int(is_left[0]) if is_left else None
            num[index + left_num] = content

    last_num = [i for i in num if i != '-']
    # print(last_num)
    result = ''.join(last_num)
    # print(result)
    return int(result)


def get_html():
    result = []
    headers = {
        'Cookie': 'footprints=eyJpdiI6Im13MWFWNzhkazJIVEZcL09qK0tES2pRPT0iLCJ2YWx1ZSI6ImVLM1JZaStcL0FRcXRNZDZZcW15SzhhSGxPM2V1TWR0RTQ0Q3ZOZ1JsSkZUWEk3eERiRGVtbFRjM3NcLzRHZUQ4SyIsIm1hYyI6IjgyZjZmOWU5Y2I0ZWFmYmU1MWIzZDQwNmE1NWEyN2M1NDU5ZDlkYTBlMjdhZjE5ZGFhZmMwMmY1MTVkZTlmNjYifQ%3D%3D; _ga=GA1.2.682296989.1618206060; _gid=GA1.2.791098064.1618206060; Hm_lvt_020fbaad6104bcddd1db12d6b78812f6=1618206060; XSRF-TOKEN=eyJpdiI6Ikx3dlFPSDk3cTZcL0YydzFaSjNSRHBnPT0iLCJ2YWx1ZSI6IlwvV244XC9ad1Fna0Rxa2lcLzFQSmRQK0c4a0hWVDV6Szh3eWtwSzljVVB5T2xqYzNFMVNhaWVURDBrRERtV296Wm4iLCJtYWMiOiJiM2RkN2M4NzI0MTQyZDIyNjdlNGU3YWYyMzJhMmU1M2JlOTRlOTEyNTgyMjEyNWNhZGI1NWUxM2RiMWMwM2FmIn0%3D; glidedsky_session=eyJpdiI6InVkMCtqcE9mSm5MR3k2dDVrcDJIemc9PSIsInZhbHVlIjoiWFwvZ0VYTmN3bnJyNlRZZ253NEFQa2F5YjJSVmEwbGxlSFkxUFZsbVo0MTIxcitcLzFZbkxnVnJDSGhcLzlQYkkwSSIsIm1hYyI6IjgyY2ExM2RmY2EyOGE0NTVjMzM0MjE3YzdiNDI0NmU2ODNlZGJmMjgxOTVhYmVlMjM3OTdiMGUzYjJhZDNiMjgifQ%3D%3D; Hm_lpvt_020fbaad6104bcddd1db12d6b78812f6=1618206461',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
    }
    for i in range(1,1001):
        time.sleep(0.2)
        url = "http://glidedsky.com/level/web/crawler-css-puzzle-1?page={}".format(i)
        print(url)
        response = requests.get(url=url, headers=headers).text
        selec = Selector(response)
        div_html = selec.xpath('//div[@class="col-md-1"]')
        for i in div_html:
            r = parse_number(div_html=i, response_str=response)
            result.append(r)
            # print(result)
    return sum(result)

if __name__ == '__main__':
    s = get_html()
    print(s)
